from django.core.validators import RegexValidator
from django.db import models
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    region = models.CharField(
        max_length=100,
        blank=True,
        verbose_name='所在地区'
    )

    school = models.CharField(
        max_length=100,
        blank=True,
        verbose_name='学校/机构'
    )

    phone_number = models.CharField(
        max_length=11,
        blank=True,
        null=True,
        unique=True,
        validators=[
            RegexValidator(
                regex=r'^1[3-9]\d{9}$',
                message='请输入有效的手机号码'
            )
        ],
        verbose_name='手机号码'
    )

    class Meta:
        verbose_name = '用户资料'
        verbose_name_plural = '用户资料'

    def __str__(self):
        return f"{self.user.username}的资料"

    def get_phone_display(self):
        """
        获取手机号的显示格式（隐藏中间部分）
        """
        if self.phone_number:
            return f"{self.phone_number[:3]}****{self.phone_number[7:]}"
        return "未绑定"
